package com.rop.order.service.impl;

import com.rop.order.mapper.OrderMapper;
import com.rop.order.pojo.Order;
import com.rop.order.pojo.User;
import com.rop.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderServiceImpl implements OrderService {
    // 使用RestTemplate来进行http请求的服务调用中使用的url
//    private static final String url = "http://localhost:8081/user/";

    // 使用了Eureka注册中心后，不能再将路径写成localhost，得写成服务名称
    private static final String url = "http://userservice/user/";

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.利用RestTemplate发送http请求，查询用户
        User user = restTemplate.getForObject(url + order.getUserId(), User.class);
        // 3. 把user封装进order
        order.setUser(user);
        // 4.返回
        return order;
    }
}
